package com.yuanian.timingwheel.bootstrap;

import com.yuanian.timingwheel.context.SpringContext;
import com.yuanian.timingwheel.facade.TwManager;
import com.yuanian.timingwheel.model.task.FixedDateTask;
import com.yuanian.timingwheel.model.task.IntervalTask;
import com.yuanian.timingwheel.model.task.build.TaskBuilder;
import com.yuanian.timingwheel.model.timingwheel.DayTimingWheel;
import com.yuanian.timingwheel.test.CommonService;
import com.yuanian.timingwheel.test.Teacher;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import java.util.Date;

@Component
public class BootStrap implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {


        TwManager.getInstance().start();

        SpringContext.getBean(CommonService.class).run2(new Teacher("haha"), new Teacher("hehe"));
        //测试
        DayTimingWheel dayTimeWheel = new DayTimingWheel();

        IntervalTask task = new IntervalTask(3, 1000000, "xxx");
        try {
            task.createTaskMethod("run", "person", new Object[]{"xxxx"});
        } catch (Exception e) {
            e.printStackTrace();
        }

        TwManager.getInstance().addTask(new TaskBuilder() {

            @Override
            public Date executionTime() {
                return new Date(new Date().getTime() + 1000);
            }

            @Override
            public String executionMethod() {
                return "run";
            }

            @Override
            public String beanName() {
                return "person";
            }

            @Override
            public Object[] args() {
                return new Object[]{new Teacher("xxx"), new Teacher("yyyy")};
            }

            @Override
            public String taskDesc() {
                return "采购合同任务";
            }

            @Override
            public String businessId() {
                return "FB0C40420DBB45019902F9A5B4C261E9";
            }

            //间隔秒数,-1为不间隔执行
            public long executionInterval() {
                return 2;
            }

            //执行次数
            public long executionRounds() {
                return 1000000;
            }
        });
        //dayTimeWheel.save(task);

        Date d = new Date(new Date().getTime() + 1000);
        for (int j = 0; j < 10; j++) {
            for (int i = 0; i < 1000; i++) {
                CommonService p = new CommonService();
                FixedDateTask task1 = new FixedDateTask("1", new Date(new Date().getTime() + i * 100), "sss");
                Teacher t1 = new Teacher("xxx");

                Teacher t2 = new Teacher("xxx");
                try {
                    task1.createTaskMethod("run", "person", new Object[]{t1, t2});
                } catch (Exception e) {
                    e.printStackTrace();
                }
                //dayTimeWheel.save(task1);
            }}
//
//             /*   FixedDateTask task = new FixedDateTask("1", i, "xxx");
//
//             /*   try {
//                    task.createTaskMethod("run", p, null);
//                    task.setTaskDesc(i + "");
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//
//                AttachTask attachTask = new AttachTask("附属任务");
//                try {
//                    attachTask.createTaskMethod("run", p, null);
//                    attachTask.setTaskDesc(i + "");
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }*/
//                //task.addAttachTask(attachTask);
//                //task.executionTime = new Date(System.currentTimeMillis() + i * 1000);
//                //dayTimeWheel.addTask(task);
//            }
//            //}
//
//
//        }





      /*  try {
            Thread.sleep(20000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        dayTimeWheel.start();*/

    }
}
